package com.lexue.dao.impl;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.lexue.dao.ISubject;
import com.lexue.domain.Subject;

/**
 * A data access object (DAO) providing persistence and search support for
 * Subject entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.lexue.dao.impl.Subject
 * @author MyEclipse Persistence Tools
 */

public class SubjectDAO extends HibernateDaoSupport implements ISubject {
	private static final Log log = LogFactory.getLog(SubjectDAO.class);
	// property constants
	public static final String SUBJECT_NAME = "subjectName";
	public static final String SUBJECT_PRICE = "subjectPrice";
	public static final String SUBJECT_TIME = "subjectTime";
	public static final String SUBJECT_WAY = "subjectWay";
	public static final String SUBJECT_STATE = "subjectState";
	public static final String OTHERS = "others";

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.ISubject#save(com.lexue.domain.Subject)
	 */
	public void save(Subject transientInstance) {
		log.debug("saving Subject instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.ISubject#delete(com.lexue.domain.Subject)
	 */
	public void delete(Subject persistentInstance) {
		log.debug("deleting Subject instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.ISubject#findById(java.lang.Integer)
	 */
	public Subject findById(java.lang.Integer id) {
		log.debug("getting Subject instance with id: " + id);
		try {
			Subject instance = (Subject) getHibernateTemplate().get(
					"com.lexue.domain.Subject", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.ISubject#findByExample(com.lexue.domain.Subject)
	 */
	public List findByExample(Subject instance) {
		log.debug("finding Subject instance by example");
		try {
			List results = getHibernateTemplate().getSessionFactory()
					.openSession().createCriteria("com.lexue.domain.Subject")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.ISubject#findByProperty(java.lang.String,
	 * java.lang.Object)
	 */
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Subject instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Subject as model where model."
					+ propertyName + "= ?";
			Query queryObject = getHibernateTemplate().getSessionFactory()
					.openSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.ISubject#findBySubjectName(java.lang.Object)
	 */
	public List findBySubjectName(Object subjectName) {
		return findByProperty(SUBJECT_NAME, subjectName);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.ISubject#findBySubjectPrice(java.lang.Object)
	 */
	public List findBySubjectPrice(Object subjectPrice) {
		return findByProperty(SUBJECT_PRICE, subjectPrice);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.ISubject#findBySubjectTime(java.lang.Object)
	 */
	public List findBySubjectTime(Object subjectTime) {
		return findByProperty(SUBJECT_TIME, subjectTime);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.ISubject#findBySubjectWay(java.lang.Object)
	 */
	public List findBySubjectWay(Object subjectWay) {
		return findByProperty(SUBJECT_WAY, subjectWay);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.ISubject#findBySubjectState(java.lang.Object)
	 */
	public List findBySubjectState(Object subjectState) {
		return findByProperty(SUBJECT_STATE, subjectState);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.ISubject#findByOthers(java.lang.Object)
	 */
	public List findByOthers(Object others) {
		return findByProperty(OTHERS, others);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.ISubject#findAll()
	 */
	public List findAll() {
		log.debug("finding all Subject instances");
		try {
			String queryString = "from Subject";
			Query queryObject = getHibernateTemplate().getSessionFactory()
					.openSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.ISubject#merge(com.lexue.domain.Subject)
	 */
	public Subject merge(Subject detachedInstance) {
		log.debug("merging Subject instance");
		try {
			Subject result = (Subject) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.ISubject#attachDirty(com.lexue.domain.Subject)
	 */
	public void attachDirty(Subject instance) {
		log.debug("attaching dirty Subject instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.ISubject#attachClean(com.lexue.domain.Subject)
	 */
	public void attachClean(Subject instance) {
		log.debug("attaching clean Subject instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}